package com.example.strategy;


import cn.hutool.core.util.StrUtil;
import com.example.entity.Coupon;
import com.example.entity.UserCoupon;
import com.example.util.BeanCopyPropertiesUtils;
import lombok.RequiredArgsConstructor;

import java.math.BigDecimal;

/**
 * 无门槛类型
 */
@RequiredArgsConstructor
public class NoThresholdDiscount implements Discount {

    private static final String RULE_TEMPLATE = "无门槛抵{}元";

    @Override
    public boolean canUse(BigDecimal totalAmount, UserCoupon coupon) {
        BigDecimal minPoint = new BigDecimal(coupon.getMinPoint());
        return totalAmount.compareTo(minPoint) > 0;
    }


    @Override
    public BigDecimal calculateDiscount(BigDecimal totalAmount, UserCoupon coupon) {
        return new BigDecimal(coupon.getReduceAmount());
    }


    @Override
    public String getRule(UserCoupon coupon) {
        return StrUtil.format(RULE_TEMPLATE, coupon.getReduceAmount());
    }

    @Override
    public String getRule(Coupon coupon) {
        UserCoupon userCoupon = BeanCopyPropertiesUtils.copyBean(coupon, UserCoupon.class);
        return getRule(userCoupon);
    }
}
